package com.my.system.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.my.system.command.impl.BUYPRODUCTCommand;
import com.my.system.command.impl.CREATEORDERCommand;
import com.my.system.command.impl.ORDERLISTCommand;
import com.my.system.command.impl.PAYORDERCommand;
import com.my.system.command.impl.USERLISTCommand;
import com.my.system.object.client.Client;
import com.my.system.object.client.User;

public class ClientCommandFactory extends CommandFactory {
	private Client client;
	private User user;
	private static Map<CommandCode, Class<? extends Command>> map = new HashMap<>();
	static {
		map.put(CommandCode.CREATEORDER, CREATEORDERCommand.class);
		map.put(CommandCode.ORDER_LIST, ORDERLISTCommand.class);
		map.put(CommandCode.USER, USERLISTCommand.class);
		map.put(CommandCode.BUYPRODUCT, BUYPRODUCTCommand.class);
		map.put(CommandCode.PAYORDER, PAYORDERCommand.class);

	}

	public ClientCommandFactory(Client client, User user) {
		// TODO Auto-generated constructor stub
		this.client = client;
		this.user = user;
	}

	@Override
	public Command buildCommand(CommandCode code) {
		// TODO Auto-generated method stub
		Class<? extends Command> command;
		command = map.get(code);
		if (command != null) {
			try {
				Constructor<? extends Command> con = command
						.getConstructor(new Class[] { Client.class, User.class });
				return con.newInstance(new Object[] { client, user });
			} catch (NoSuchMethodException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return new SystemCommandFactory(client, user).buildCommand(code);

	}

}
